package com.atguigu.paymentdemo.controller;

import com.atguigu.paymentdemo.entity.CourseInfo;
import com.atguigu.paymentdemo.service.CourseFileService;
import com.atguigu.paymentdemo.service.CourseInfoService;
import com.atguigu.paymentdemo.util.AjaxResult;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;


@Api(tags = "课程表Controller")
@RestController
@RequestMapping("courseInfo")
@AllArgsConstructor
public class CourseInfoController {

	private final CourseInfoService courseInfoService;

	private final CourseFileService courseFileService;

	/**
	 * 分页查询
	 *
	 * @param page       分页对象
	 * @param courseInfo /
	 * @return /
	 */
	@ApiOperation(value = "分页查询")
	@GetMapping(value = "/page", produces = {"application/json;charset=UTF-8"})
	public AjaxResult getCourseInfoPage(Page<CourseInfo> page, CourseInfo courseInfo) {
		return AjaxResult.success(courseInfoService.page(page, Wrappers.query(courseInfo)));
	}

	/**
	 * 不分页查询
	 *
	 * @param courseInfo 课程
	 * @return /
	 */
	@ApiOperation(value = "不分页查询")
	@GetMapping(value = "/list", produces = {"application/json;charset=UTF-8"})
	public AjaxResult getCourseInfoList(CourseInfo courseInfo) {
		return AjaxResult.success(courseInfoService.list(courseInfo));
	}

	/**
	 * 根据讲师id查询-不分页
	 *
	 * @param id 讲师id
	 * @return /
	 */
	@ApiOperation(value = "根据讲师id查询课程-不分页")
	@GetMapping(value = "/getListByTeacherId/{id}", produces = {"application/json;charset=UTF-8"})
	public AjaxResult getListByTeacherId(@PathVariable("id") Long id) {
		return AjaxResult.success(courseInfoService.getByTeacherId(id));
	}

	/**
	 * 通过id查询课程详情
	 *
	 * @param id /
	 * @return R
	 */
	@ApiOperation(value = "通过id查询课程详情")
	@GetMapping(value = "/{id}", produces = {"application/json;charset=UTF-8"})
	public AjaxResult getById(@PathVariable("id") String id) {
		return AjaxResult.success(courseInfoService.getVoById(id));
	}

	/**
	 * 通过id查询简介图片列表
	 *
	 * @param id /
	 * @return R
	 */
	@ApiOperation(value = "通过id查询简介图片列表")
	@GetMapping(value = "/getImageListById/{id}", produces = {"application/json;charset=UTF-8"})
	public AjaxResult getImageListById(@PathVariable("id") String id) {
		return AjaxResult.success(courseFileService.getImage(id));
	}

	/**
	 * 通过id查询视频列表
	 *
	 * @param id /
	 * @return R
	 */
	@ApiOperation(value = "通过id查询视频列表")
	@GetMapping(value = "/getVideoListById/{id}", produces = {"application/json;charset=UTF-8"})
	public AjaxResult getVideoListById(@PathVariable("id") String id) {
		return AjaxResult.success(courseFileService.getVideo(id));
	}

	/**
	 * 新增课程
	 *
	 * @param courseInfo 课程
	 * @return R
	 */
	@ApiOperation(value = "新增课程")
	@PostMapping(produces = {"application/json;charset=UTF-8"})
	public AjaxResult save(@RequestBody CourseInfo courseInfo) {
		courseInfoService.save(courseInfo);
		return AjaxResult.success("新增成功", courseInfo.getId());
	}

	/**
	 * 修改课程
	 *
	 * @param CourseInfo 课程
	 * @return R
	 */
	@ApiOperation(value = "修改课程")
	@PutMapping(produces = {"application/json;charset=UTF-8"})
	public AjaxResult updateById(@RequestBody CourseInfo CourseInfo) {
		return AjaxResult.success(courseInfoService.updateById(CourseInfo));
	}

	/**
	 * 通过id删除课程
	 *
	 * @param id /
	 * @return R
	 */
	@ApiOperation(value = "通过id删除课程")
	@DeleteMapping(value = "/{id}", produces = {"application/json;charset=UTF-8"})
	public AjaxResult removeById(@PathVariable String id) {
		return AjaxResult.success(courseInfoService.removeById(id));
	}


	/**
	 * 上传课程视频
	 *
	 * @param file 文件
	 * @param id   /
	 * @return R
	 */
	@ApiOperation(value = "上传课程视频")
	@PostMapping(value = "/addVideo", produces = {"application/json;charset=UTF-8"})
	public AjaxResult addVideo(MultipartFile file, @PathVariable Long id) throws IOException {
		courseInfoService.addVideo(file, id);
		return AjaxResult.success();
	}

	/**
	 * 删除oss文件
	 *
	 * @param id 文件id
	 * @return R
	 */
	@ApiOperation(value = "删除oss文件")
	@GetMapping(value = "/delOssFile/{id}", produces = {"application/json;charset=UTF-8"})
	public AjaxResult delOssFile(@PathVariable Long id) {
		courseInfoService.delOssFile(id);
		return AjaxResult.success();
	}

}
